package org.demoth.arkanoid;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.swing.*;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;

import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 * User: daniil
 * Date: 20.05.11
 * Time: 22:45
 * Loads everything
 */
public class ResourceManager {
    public static int width = 1024;
    public static int height = 768;
    public static int mouseX;

    public static void loadConfig(String path) {
        try {
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(path));
            NodeList nodeList = document.getElementsByTagName("param");
            Node node = nodeList.item(0);
            width = Integer.parseInt(node.getAttributes().getNamedItem("width").getNodeValue());
            height = Integer.parseInt(node.getAttributes().getNamedItem("height").getNodeValue());

        } catch (Exception e) {
            e.getMessage();
        }

    }

    public static Scene loadScene(String path) {
        Scene scene = new Scene();
        scene.gameObjects = new ArrayList<GameObject>();
        scene.breakables = new ArrayList<Breakable>();

        try {
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(path));
            // Reading Breakables (or bricks)
            NodeList nodeList = document.getElementsByTagName("brick");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Breakable breakable = new Breakable();
                Node node = nodeList.item(i);
                breakable.x = Float.parseFloat(node.getAttributes().getNamedItem("x").getNodeValue());
                breakable.y = Float.parseFloat(node.getAttributes().getNamedItem("y").getNodeValue());
                breakable.dropRate = Float.parseFloat(node.getAttributes().getNamedItem("dropRate").getNodeValue());
                breakable.level = Integer.parseInt(node.getAttributes().getNamedItem("level").getNodeValue());
                breakable.graphics = new ImageIcon("data/brick.png").getImage();

                // little redundancy, but that's ok
                scene.breakables.add(breakable);
                scene.gameObjects.add(breakable);

            }
            // Reading player
            nodeList = document.getElementsByTagName("player");
            Player player = new Player();
            Node node = nodeList.item(0);
            player.lives = Integer.parseInt(node.getAttributes().getNamedItem("lives").getNodeValue());
            player.y = height - Integer.parseInt(node.getAttributes().getNamedItem("y").getNodeValue());
            scene.player = player;
            scene.gameObjects.add(player);

            // Ball
            GameObject ball = new GameObject() {
                @Override
                public void update(float time) {
                    if (x < 0 || x > width) {
                        vx *= -1;
                    }
                    x += vx * time;
                    y += vy * time;
                }
            };
            ball.y = player.y - 50;
            scene.gameObjects.add(ball);

            // Paddle
            GameObject paddle = new GameObject() {
                @Override
                public void update(float time) {
                    x = ResourceManager.getMouseX();
                    System.out.println("x = " + x);
                }
            };
            paddle.y = player.y;
            scene.gameObjects.add(paddle);


        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return scene;
    }

    public static int getMouseX() {
        return mouseX;
    }
    public static void setMouseX(int newVal) {
        mouseX = newVal;
    }
}
